/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-11-14
 * Time:11:04
 */
class BinarySearchTree{
    static class  TreeNode{
        public  int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }

    public TreeNode root;

    public TreeNode findVal(int key){
        if(root == null)return  null;
        TreeNode cur = root;
        while (cur != null){
            if(cur.val < key){
                cur = cur.right;
            }else if(cur.val == key){
                return  cur;
            }else{
                cur = cur.left;
            }
        }
        return  null;
    }

    //判断有没有这个结点
    //找到合适的位置进行插入操作
    public  void insert(int key){
        TreeNode treeNode = new TreeNode(key);
        TreeNode parent = null;
        TreeNode cur = root;
        if(root == null){
            root = treeNode；
            return;
        }
        while (cur != null){
            if(cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if(cur.val == key){
                return;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        if(parent.val > key){
            parent.left = treeNode;
        }else{
            parent.right = treeNode;
        }
    }
    public  void remove(int key){
        //找到要删除的结点，有可能没有
        //调整前后的联接，此时就需要一个父节点记录位置
        TreeNode parent = null;
        TreeNode cur = root;
        if (root == null)return;
        while (cur != null){
            if(cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if(cur.val == key){//找到了要删除的结点进行删除操作
                removeNode(parent,cur);
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
    }
    private void removeNode(TreeNode parent,TreeNode cur){
        //删除二叉搜索树
        //3种情况
        //cur.left == null     cur == root //  cur == parent.left // cur == parent.right
        //cur.right == null    cur == root //  cur == parent.left // cur == parent.right
        //cur.left != null && cur.right != null  替罪羊法  当前结点的左树的右树中找最小值  当前结点的右树的左树中找最大值
        if(cur.left == null){
            if(cur == root){
                root = root.right;
            }else if(cur == parent.left){
                parent.left = cur.right;
            }else{
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if(cur == null){
                root = root.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{
            TreeNode tp = cur;
            TreeNode tar = cur.right;
            while (tar.left != null){
                tp = tar;
                tar = tar.left;
            }
            cur.val = tar.val;
            if(tar == tp.left){
                tp.left = tar.right;
            }else{
                tp.right = tar.right;
            }
        }
    }
}
public class Test {
    public static void main(String[] args) {

    }
}
